<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>权限管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/zTree/css/zTreeStyle/zTreeStyle.css}" >
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">功能名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" maxlength="100" lay-reqtext="名称不能为空" placeholder="请输入功能名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">功能链接</label>
        <div class="layui-input-block">
            <input type="text" name="url"  maxlength="500" lay-reqtext="链接不能为空" placeholder="请输入功能链接" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">权限标识</label>
        <div class="layui-input-block">
            <input type="text" name="permission" lay-verify="required" maxlength="255" lay-reqtext="权限标识不能为空" placeholder="请输入功能权限标识" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">功能类型</label>
        <div class="layui-input-block">
            <input type="radio" name="type" value="0" title="目录" checked="">
            <input type="radio" name="type" value="1" title="菜单">
            <input type="radio" name="type" value="2" title="操作按钮" >
            <input type="radio" name="type" value="3" title="工具条按钮" >
            <input type="radio" name="type" value="4" title="API" >
            <input type="radio" name="type" value="5" title="页面控件" >
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">功能内容</label>
        <div class="layui-input-block">
            <textarea name="functionText" class="layui-textarea"  placeholder="功能内容"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">上级功能</label>
        <div class="layui-input-block">
            <input type="hidden" name="pid" id="pid">
            <input type="hidden" name="appCode" id="appCode">
            <input type="text" readonly  id="selectPid" lay-verify="required"  placeholder="请选择上级功能" value="" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label ">图标</label>
        <div class="layui-input-block">
            <input type="text" name="icon"  maxlength="60"  placeholder="请输入功能图标" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">排序</label>
        <div class="layui-input-block">
            <input type="number" name="functionSort"    placeholder="请输入排序" value="" class="layui-input" lay-verify="number">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="enableStatus" value="1" title="启用" checked="">
            <input type="radio" name="enableStatus" value="0" title="禁用">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注信息</label>
        <div class="layui-input-block">
            <textarea name="remark" class="layui-textarea" maxlength="255" placeholder="请输入备注信息"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script th:src="@{/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script>
    layui.use(['form','layer','tree','jquery'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        var index;
        var tree = layui.tree;
        var loadding;
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url:"[[@{/function/save}]]",
                contentType: "application/json; charset=utf-8",
                type:'POST',
                data:JSON.stringify(data.field),
                success:function(data){
                    if(data.code==0) {
                        layer.msg(data.msg);
                        return false;
                    }
                    var index = layer.alert(data.msg, {
                        title: '提示信息'
                    }, function () {
                        // 关闭弹出层
                        layer.close(index);
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.location.reload();
                        parent.layer.close(iframeIndex);

                    });
                }
            });
            return false;
        });


        var setting = {
            async: {
                enable: true,
                url:"[[@{/}]]function/query/app/function/tree",
                autoParam:["id", "pid", "name","appCode","isAppNode"],
                dataFilter: filter
            },
            data:{
                simpleData:{
                    enable: true,
                    idKey:'id',
                    pIdKey:'parentId',
                    rootPId: -2
                }
            },
            view:{
                showIcon: true
            },
            callback: {
                onClick: functionTreeClick
            }
        };


        function filter(treeId, parentNode, data) {
            var dataArray = data.data;
            if(dataArray!=null&&dataArray.length>0)
            {
                for(var i=0;i<dataArray.length;i++)
                {
                    dataArray[i].url=null;
                }
            }
            return dataArray;
        }
        function functionTreeClick(event, treeId, treeNode) {
            // $("#inp").val("id="+treeNode.id + ", name=" + treeNode.name + ",pId=" + treeNode.pId);

            $("#selectPid").val(treeNode.name);
            if(treeNode.isAppNode)
            {
                $("#pid").val("-1");
            }else {
                $("#pid").val(treeNode.id);
            }
            $("#appCode").val(treeNode.appCode);
            layer.close(index);
        }



        $("#selectPid").click(function(){
            index = layer.open({
                title: '选择上级功能',
                type: 1,
                shade: 0.2,
                maxmin:true,

                area: ['20%', '60%'],
                content: "<ul id=\"appFunctionTree\" style='height:90%' class=\"ztree\"></ul>",
                success:function(layero){
                    loadding = layer.load();

                    $.ajax({
                        url:"[[@{/}]]function/query/app/function/tree",
                        type:"POST",
                        dataType: "json",
                        success: function(data){
                            var zTreeObj = $.fn.zTree.init($("#appFunctionTree"),setting, data.data);
                        },
                        error: function(){

                        },
                        complete:function()
                        {
                            layer.close(loadding);
                        }
                    });

                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });

        });
    });
</script>


<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.core.js}"></script>
<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.excheck.js}"></script>
<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.exedit.js}"></script>


</body>
</html>